const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class PlatformRedirectHandler extends BaseHandler {

    get pattern() {
        return "/wechat/platform/redirect";
    }

    
    doGET(request, response) {
        var redirectUri = `http://${request.headers['host']}/wechat/platform/authorize`;
        this.Controller.getPlatformOAuthURL(redirectUri, (url) => {
            if(url === 'FAILED') {
                response.end('Try later');
            } else {
                response.redirect(url);
            }
        });
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = PlatformRedirectHandler;